//
//  BezierByForward.cpp
//  client
//
//  Created by DuanHongbo on 15/1/29.
//
//

#include "BezierByForward.h"

BezierByForward::BezierByForward()
:m_autoHeadOn(false),
m_initAngle(0) {
}

BezierByForward::~BezierByForward() {
}

bool BezierByForward::initWithDuration(float duration,const ccBezierConfig& c,bool autoHeadOn , float initAngle) {
    if (BezierBy::initWithDuration(duration, c)) {
        m_autoHeadOn = autoHeadOn;
        m_initAngle = initAngle;
        return true;
    }
    
    return false;
}

BezierByForward* BezierByForward::create(float duration,const ccBezierConfig& c,bool autoHeadOn, float initAngle) {
    BezierByForward* j = new BezierByForward();
    if(j->initWithDuration(duration, c, autoHeadOn, initAngle)) {
        return (BezierByForward*)j->autorelease();
    }
    
    j->release();
    return j;
}

BezierByForward* BezierByForward::clone() const {
   	auto a = new (std::nothrow) BezierByForward();
    a->initWithDuration(_duration, _config,m_autoHeadOn,m_initAngle);
    a->autorelease();
    return a;
}

void BezierByForward::update(float time) {
    if (_target)
    {
        Vec2 oldPos = _previousPosition;
        BezierBy::update(time);
        Vec2 newPos = _target->getPosition();
        
        // auto head on
        if(m_autoHeadOn) {
            Vec2 v = newPos-oldPos;
            float r = v.getAngle();
            float d = -CC_RADIANS_TO_DEGREES(r);
            d += m_initAngle;
            _target->setRotation(d);
        }
    }
}

BezierByForward* BezierByForward::reverse() const{
    ccBezierConfig r;
    r.endPosition = -_config.endPosition;
    r.controlPoint_1 = _config.controlPoint_2 + (-_config.endPosition);
    r.controlPoint_2 = _config.controlPoint_1 + (-_config.endPosition);
    return BezierByForward::create(getDuration(), r, m_autoHeadOn, 180 - m_initAngle);
}
